<template>
	<view>
		<text>授权登录</text>
	</view>
</template>

<script>
	import appId from '@/utils/config.js'

	export default {
		onLoad(options) {
			console.log(options)
			// if (options.code) {
			// 	this.autoLogin(code)
			// } else {
			// 	this.toAuthorizePage()
			// }
			// let openId = this.openId = uni.getStorageSync("openId")
			// if (openId) {
			// 	this.autoLogin()
			// } else {
			// 	this.toAuthorizePage()
			// }
		},
		data() {
			return {
				openId: ''
			}
		},
		methods: {
			toAuthorizePage(stateObj = {}) {
				// https://open.weixin.qq.com/connect/oauth2/authorize 是微信公众号官方提供的授权页面
				// 微信授权页面授权成功并获取到code后，自动跳转至redirect_uri/?code=CODE&state=STATE，redirect_uri参数需encodeURIComponent格式化，且注意二级路径
				// 应用授权作用域，snsapi_base直接挑战，snsapi_userinfo需用户显式授权
				// state为跳转至redirect_uri附带的参数，最多128字节
				let redirect_uri = location.origin + location.pathname + '#/pages/login/authorize'
				console.log(redirect_uri)
				let scope = 'snsapi_userinfo'
				let state = btoa(JSON.stringify(stateObj))
				location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize' +
					'?appid=' + appId +
					'&redirect_uri=' + encodeURIComponent(redirect_uri) +
					'&response_type=code&scope=' + scope +
					'&state=' + state +
					'#wechat_redirect';
			},
			autoLogin(code) {
				// 自动登录，拿着openId去后台查询
				this.$request.get('/wechat/test/authUser', {code})
			}
		}
	}
</script>

<style>

</style>